package testJdk.nio.testCharset;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * @author cyd
 * @version 1.0.0
 * @date 2021/11/27 17:45
 */


public class CharsetDemo {

    @Test
    public void test_forName() {
        Charset charset = Charset.forName("UTF-8");
        System.out.println(charset);
        System.out.println("----------");
        Charset defaultCharset = Charset.defaultCharset();
        System.out.println(defaultCharset);
    }

    @Test
    public void test_isSupport() {
        boolean xxx = Charset.isSupported("xxx");
        System.out.println(xxx);
    }

    @Test
    public void test_availableCharsets() {
        SortedMap<String, Charset> stringCharsetSortedMap = Charset.availableCharsets();
        Set<String> strings = stringCharsetSortedMap.keySet();
        strings.forEach(System.out::println);
    }

    @Test
    public void charSetEncoderAndDecoder() throws
            CharacterCodingException {
        Charset charset = Charset.forName("UTF-8");
        //1.获取编码器
        CharsetEncoder charsetEncoder = charset.newEncoder();
        //2.获取解码器
        CharsetDecoder charsetDecoder = charset.newDecoder();
        //3.获取需要解码编码的数据
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        charBuffer.put("字符集编码解码");
        charBuffer.flip();
        //4.编码
        ByteBuffer byteBuffer = charsetEncoder.encode(charBuffer);
        System.out.println("编码后：");
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.println(byteBuffer.get());
        }
        //5.解码
        byteBuffer.flip();
        CharBuffer charBuffer1 = charsetDecoder.decode(byteBuffer);
        System.out.println("解码后：");
        System.out.println(charBuffer1.toString());
        System.out.println("指定其他格式解码:");
        Charset charset1 = Charset.forName("GBK");
        byteBuffer.flip();
        CharBuffer charBuffer2 = charset1.decode(byteBuffer);
        System.out.println(charBuffer2.toString());
        //6.获取 Charset 所支持的字符编码
        Map<String, Charset> map = Charset.availableCharsets();
        Set<Map.Entry<String, Charset>> set = map.entrySet();
        for (Map.Entry<String, Charset> entry : set) {
            System.out.println(entry.getKey() + "=" + entry.getValue().toString());
        }
    }
}
